Programming Embedded Systems by Michael Barr & Anthony Massa

Programming Embedded Systems by Michael Barr & Anthony Massa

Author:Michael Barr & Anthony Massa [Michael Barr]
Language: eng
Format: epub
Tags: COMPUTERS / Hardware / General
ISBN: 9780596553289
Publisher: O'Reilly Media
Published: 2009-02-08T16:00:00+00:00


Struct Overlays

In embedded systems featuring memory-mapped I/O devices, it is sometimes useful to overlay a C struct onto each peripheral’s control and status registers. Benefits of struct overlays are that you can read and write through a pointer to the struct, the register is described nicely by the struct, code can be kept clean, and the compiler does the address construction at compile time.

The following example code shows a struct overlay for a timer peripheral. If a peripheral’s registers do not align correctly, reserved members can be included in the struct. Thus, in the following example, an extra field that you’ll never refer to is included at offset 4 so that the control field lies properly at offset 6.

typedef struct { uint16_t count; /* Offset 0 */ uint16_t maxCount; /* Offset 2 */ uint16_t _reserved1; /* Offset 4 */ uint16_t control; /* Offset 6 */ } volatile timer_t; timer_t *pTimer = (timer_t *)(0xABCD0123);



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.